package com.zyw.crawler.services;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.zyw.util.WordCount;

public class PdfReader {
	public static void main(String[] args) {
//		String pdfPath="D:/apache-tomcat-8.0.47/webapps/CrawlerForGzxt/WEB-INF/classes/cacheFile/88b793fc-35e2-41ed-bd1d-5994bbbd803e.pdf";
//		File f=new File(pdfPath);
//		if(f.exists()){
//			System.out.println(getTextFromPdf(pdfPath));
//		}
		System.out.println("start task");
		CompanyFileInfo cfi=new CompanyFileInfo();
		cfi.path="D:/apache-tomcat-8.0.47/webapps/CrawlerForGzxt/WEB-INF/classes/cacheFile/";
		cfi.cacheName="88b793fc-35e2-41ed-bd1d-5994bbbd803e.pdf";
		List<CompanyFileInfo> fs=new ArrayList<CompanyFileInfo>();
		fs.add(cfi);
		InfoWordCountFromPdfs(fs, "D:/a.xlsx");
	}
    /** 
     *  
     * @Title: getTextFromPdf 
     * @Description: 读取pdf文件内容 
     * @param filePath 
     * @return: 读出的pdf的内容 
     */  
    public static String getTextFromPdf(String filePath) {  
        String result = null;  
        FileInputStream is = null;  
        PDDocument document = null;  
        try {  
            is = new FileInputStream(filePath);  
            PDFParser parser = new PDFParser(is);  
            parser.parse();  
            document = parser.getPDDocument();  
            PDFTextStripper stripper = new PDFTextStripper();  
            
            result = stripper.getText(document);  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (is != null) {  
                try {  
                    is.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            if (document != null) {  
                try {  
                    document.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }
            }  
        }  
        return result;  
    }  
    /**
     * 获取pdf多少页
     * @param path
     * @return
     */
	public static Integer getPDfPageSize(String path){
    	Integer size=-1;
    	FileInputStream is = null;  
        PDDocument document = null;  
        try {
        	is = new FileInputStream(path);  
            PDFParser parser = new PDFParser(is);  
            parser.parse();  
            document = parser.getPDDocument();
            size=document.getNumberOfPages();
		} catch (Exception e) {
		}
    	return size;
    }
	/**
	 * 电话号,手机号,邮箱全部按各自格式抓取,不按出现关键字抓
	 */
	public static String [] defGuanJianZi={"#存在邮箱","董事会秘书","#固定电话",
			"#手机号","主办券商","邮编","证券代码",
			"邮箱","联系方式","电话","营业收入","律师"};
	/**
	 * 从多个pdf中 按照关键字搜索需要的信息
	 * 需要内容：编号   关键字   关键字所在行内容  出现关键字文件,出现的页码,出现的行数
	 * 
	 * 排序:按时间排序 按最近到最前
	 * @param fileInfos 文件信息列表
	 * @param gjzs
	 * @param resultSavePath
	 */
	public static void findInfoFromPdfs(List<CompanyFileInfo> fileInfos, List<String> gjzs, String resultSavePath) {
		
		 	FileInputStream is = null;  
	        PDDocument document = null; 
	        List<PdfGjzResultInfo> result=new ArrayList<PdfGjzResultInfo>();
	        for(CompanyFileInfo fileInfo:fileInfos){
	        	try {
		            is = new FileInputStream(fileInfo.path+fileInfo.cacheName);  
		            PDFParser parser = new PDFParser(is);  
		            parser.parse();
		            document = parser.getPDDocument(); 
		            PDFTextStripper stripper = new PDFTextStripper();
		            //获取pdf有多少页
		            Integer pageCount=document.getNumberOfPages();
		            //循环获取每页信息
		            for(int i=0;i<pageCount;i++){
		            	stripper.setStartPage(i);
		            	stripper.setEndPage(i);
		            	String [] rows=stripper.getText(document).split("\r\n");
		            	for(int j=0;j<rows.length;j++){
		            		rows[j]=" "+rows[j]+" ";//前后加下空格,避免存在开头字段,正则无法匹配
		            		for(String gjz:gjzs){
		            			if(rows[j]==null||gjz==null){
		            				continue;
		            			}
		            			//获取此行是否出现邮箱
		            			if(Pattern.matches(".*([a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}).*",rows[j])&&gjz.equals("#存在邮箱")){
		            				PdfGjzResultInfo r=new PdfGjzResultInfo(gjz,rows[j],j,i,fileInfo.title);
		            				result.add(r);
		            				continue;
		            			}
		            			//获取此行是否出现电话号
		            			if(Pattern.matches(".*(\\d{3}-[1-9]{1}[0-9]{5,8}).*",rows[j])&&gjz.equals("#固定电话")){
		            				PdfGjzResultInfo r=new PdfGjzResultInfo(gjz,rows[j],j,i,fileInfo.title);
		            				result.add(r);
		            				continue;
		            			}
		            			//获取此行是否出现手机号
		            			if(Pattern.matches(".*(1[3|4|5|8][0-9]\\d{8}).*",rows[j])&&gjz.equals("#手机号")){
		            				PdfGjzResultInfo r=new PdfGjzResultInfo(gjz,rows[j],j,i,fileInfo.title);
		            				result.add(r);
		            				continue;
		            			}
		            			//判断是否存在关键字(通用判断)
		            			if(rows[j].contains(gjz)){
		            				PdfGjzResultInfo r=new PdfGjzResultInfo(gjz,rows[j],j,i,fileInfo.title);
		            				result.add(r);
		            				continue;
		            			}
		            		}
		            	}
		            }
		        }catch(Exception e){
		        	e.printStackTrace();
		        }
	        }
	        XSSFWorkbook xssf=null;
			try {
				xssf=new XSSFWorkbook();//创建xlsx 工作空间
				XSSFSheet sheet=xssf.createSheet();//创建一个空白页
				XSSFRow titleRow=sheet.createRow(0);//创建标题行
				titleRow.createCell(0).setCellValue("编号");
				titleRow.createCell(1).setCellValue("关键字");
				titleRow.createCell(2).setCellValue("匹配内容");
				titleRow.createCell(3).setCellValue("所在文件");
				titleRow.createCell(4).setCellValue("所在页");
				titleRow.createCell(5).setCellValue("所在行");
				int rn=1;
				for (int i=0;i<result.size();i++) {//循环读取属于当前公司的文件信息
					XSSFRow row=sheet.createRow(rn);//创建行
					//以下为文件名处理
					PdfGjzResultInfo r=result.get(i);
					if(r.gjz==null||r.gjz.trim().equals("")){
						continue;
					}
					row.createCell(0).setCellValue(i);
					row.createCell(1).setCellValue(r.gjz);
					row.createCell(2).setCellValue(r.rowInfo);
					row.createCell(3).setCellValue(r.fileTitle);
					row.createCell(4).setCellValue(r.pageCount);
					row.createCell(5).setCellValue(r.rowCount);
					rn++;
				}
				//输出文件
				if(!new File(resultSavePath).exists())new File(resultSavePath).createNewFile();
				FileOutputStream outputStream = new FileOutputStream(resultSavePath);
				xssf.write(outputStream);
				outputStream.flush();
				xssf.close();//关闭文件
			} catch (Exception e) {
				e.printStackTrace();
			}
	        
	}
	/**
	 * 文本词频统计
	 * @param fileInfo
	 * @param strings
	 * @param string
	 */
	public static void InfoWordCountFromPdfs(List<CompanyFileInfo> fileInfos,String resultSavePath) {
		Map<String,Integer> resultMap=new HashMap<String, Integer>();
		for(CompanyFileInfo fileInfo:fileInfos){
			FileInputStream is = null;  
	        PDDocument document = null; 
	        
	        try {
	        	
	        	is = new FileInputStream(fileInfo.path+fileInfo.cacheName);  
	            PDFParser parser = new PDFParser(is);  
	            parser.parse();  
	            document = parser.getPDDocument();  
	            PDFTextStripper stripper = new PDFTextStripper();
	            resultMap=WordCount.count(stripper.getText(document),resultMap);
	           
			} catch (Exception e) {
				
			}
		}
		 XSSFWorkbook xssf=null;
			try {
				xssf=new XSSFWorkbook();//创建xlsx 工作空间
				XSSFSheet sheet=xssf.createSheet();//创建一个空白页
				XSSFRow titleRow=sheet.createRow(0);//创建标题行
				titleRow.createCell(0).setCellValue("词语");
				titleRow.createCell(1).setCellValue("出现次数");
				titleRow.createCell(2).setCellValue("所在文件");
				Object[] keyArray=resultMap.keySet().toArray();
				for(int i=0;i<keyArray.length;i++){
					XSSFRow row=sheet.createRow(i+1);
					String k=keyArray[i].toString();
					row.createCell(0).setCellValue(k);
					row.createCell(1).setCellValue(resultMap.get(k));
				}
				//输出文件
				if(!new File(resultSavePath).exists())new File(resultSavePath).createNewFile();
				FileOutputStream outputStream = new FileOutputStream(resultSavePath);
				xssf.write(outputStream);
				outputStream.flush();
				xssf.close();//关闭文件
			} catch (Exception e) {
				e.printStackTrace();
			}
		
	}
	public static void saveAllInfoInText(List<CompanyFileInfo> fileInfos, String filePath) {
		if(new File(filePath).exists()){
			new File(filePath).delete();//存在文件则删除
		}
		try {
			new File(filePath).createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(CompanyFileInfo fileInfo:fileInfos){
			FileInputStream is = null;  
	        PDDocument document = null; 
	        writeInfoInText(filePath,"*****************************分隔线******************************");
	        writeInfoInText(filePath,"\r\n>>>文件名->"+fileInfo.title);
	        writeInfoInText(filePath,"\r\n>>>文件原始地址->"+fileInfo.fileUrl);
	        writeInfoInText(filePath,"\r\n>>>文件在股转系统上传时间->"+fileInfo.date);
	        try {
	        	is = new FileInputStream(fileInfo.path+fileInfo.cacheName);  
	            PDFParser parser = new PDFParser(is);  
	            parser.parse();  
	            document = parser.getPDDocument();  
	            PDFTextStripper stripper = new PDFTextStripper();
	            //获取pdf有多少页
	            Integer pageCount=document.getNumberOfPages();
	            //循环获取每页信息
	            for(int i=0;i<pageCount;i++){
	            	stripper.setStartPage(i);
	            	stripper.setEndPage(i);
	            	String pageInfo=stripper.getText(document);
	            	writeInfoInText(filePath,"\r\n>>>页码:"+(i+1)+"/"+pageCount);
	            	writeInfoInText(filePath,pageInfo);
	            }
		        writeInfoInText(filePath,"\r\n*****************************当前文件结束******************************");
			} catch (Exception e) {
				
			}
		}
	}
	static void writeInfoInText(String filePath,String info){
		 BufferedWriter out = null;     
		 if(!new File(filePath).exists())
		 {
			 try {
					new File(filePath).createNewFile();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		 }
	        try {     
	            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true)));     
	            out.write(info);     
	        } catch (Exception e) {     
	            e.printStackTrace();     
	        } finally {     
	            try {     
	                if(out != null){  
	                    out.close();     
	                }  
	            } catch (IOException e) {     
	                e.printStackTrace();     
	            }     
	        } 
	}
}
class PdfGjzResultInfo{
	String gjz;
	String rowInfo;
	Integer rowCount;
	Integer pageCount;
	String fileTitle;
	public PdfGjzResultInfo() {
		super();
	}
	public PdfGjzResultInfo(String gjz, String rowInfo, Integer rowCount, Integer pageCount, String fileTitle) {
		super();
		this.gjz = gjz;
		this.rowInfo = rowInfo;
		this.rowCount = rowCount;
		this.pageCount = pageCount;
		this.fileTitle = fileTitle;
	}
	public String getGjz() {
		return gjz;
	}
	public void setGjz(String gjz) {
		this.gjz = gjz;
	}
	public String getRowInfo() {
		return rowInfo;
	}
	public void setRowInfo(String rowInfo) {
		this.rowInfo = rowInfo;
	}
	public Integer getRowCount() {
		return rowCount;
	}
	public void setRowCount(Integer rowCount) {
		this.rowCount = rowCount;
	}
	public Integer getPageCount() {
		return pageCount;
	}
	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}
	public String getFileTitle() {
		return fileTitle;
	}
	public void setFileTitle(String fileTitle) {
		this.fileTitle = fileTitle;
	}
	
}